package com.zeropol2.anyquestion.component.ui.widget;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zeropol2.anyquestion.R;

/**
 * 페이지 인디케이터 컴포넌트 << < 1 2 3 4 5 > >>
 * 
 * @author zeropol2
 * 
 */
public class PageIndicatorView extends LinearLayout {
	private Context mContext;
	private LayoutInflater mInflater;
	private LinearLayout mLayout;
	public static final int ARTICLE_COUNT_PER_PAGE = 20;
	public static final int PAGE_INDICATOR_COUNT_PER_PAGE = 5;
	private int mTotalPageCount;
	private int mFromCount;
	private TextView mGoFirst;
	private TextView mGoPrev;
	private TextView mGoNext;
	private TextView mGoLast;
	private TextView mCurrentCountTv;
	private View.OnClickListener mClickListener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.page_indicator_view_go_first:
				mPageIndicatorClickListener.onGoFirstClick();
				break;
			case R.id.page_indicator_view_go_last:
				mPageIndicatorClickListener.onGoLastClick();
				mPageIndicatorClickListener.onGoCountClick(mTotalPageCount);
				setCurrentPageCount(mTotalPageCount);
				break;
			case R.id.page_indicator_view_go_next:
				mPageIndicatorClickListener.onGoNextClick();
				break;
			case R.id.page_indicator_view_go_prev:
				mPageIndicatorClickListener.onGoPrevClick();
				break;
			case R.id.page_indicator_view_go_0:
				mPageIndicatorClickListener.onGoCountClick(mFromCount);
				setCurrentPageCount(mFromCount);
				break;
			case R.id.page_indicator_view_go_1:
				mPageIndicatorClickListener.onGoCountClick(mFromCount + 1);
				setCurrentPageCount(mFromCount + 1);
				break;
			case R.id.page_indicator_view_go_2:
				mPageIndicatorClickListener.onGoCountClick(mFromCount + 2);
				setCurrentPageCount(mFromCount + 2);
				break;
			case R.id.page_indicator_view_go_3:
				mPageIndicatorClickListener.onGoCountClick(mFromCount + 3);
				setCurrentPageCount(mFromCount + 3);
				break;
			case R.id.page_indicator_view_go_4:
				mPageIndicatorClickListener.onGoCountClick(mFromCount + 4);
				setCurrentPageCount(mFromCount + 4);
				break;
			}
		}
	};

	public static interface PageIndicatorClickListener {
		public void onGoFirstClick();

		public void onGoPrevClick();

		public void onGoNextClick();

		public void onGoLastClick();

		public void onGoCountClick(int location);
	}

	private PageIndicatorClickListener mPageIndicatorClickListener;

	public PageIndicatorView(Context context) {
		super(context);
		mContext = context;
		init();
	}

	public PageIndicatorView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		init();
	}

	private void init() {
		mInflater = (LayoutInflater) getContext().getSystemService(
				Context.LAYOUT_INFLATER_SERVICE);
		mLayout = (LinearLayout) mInflater.inflate(
				R.layout.page_indicator_view, this);
		mGoFirst = (TextView) mLayout
				.findViewById(R.id.page_indicator_view_go_first);
		mGoPrev = (TextView) mLayout
				.findViewById(R.id.page_indicator_view_go_prev);
		mGoNext = (TextView) mLayout
				.findViewById(R.id.page_indicator_view_go_next);
		mGoLast = (TextView) mLayout
				.findViewById(R.id.page_indicator_view_go_last);
	}

	/**
	 * pageIndicator를 설정
	 * 
	 * @param totalArticleCount
	 *            //전체 글 목록
	 */
	public void setTotalPageCount(int totalArticleCount) {
		// pageIndicator가 몇번까지 갈지 설정
		mTotalPageCount = totalArticleCount / ARTICLE_COUNT_PER_PAGE;

		// 5개보다 많으면
		if (PAGE_INDICATOR_COUNT_PER_PAGE < mTotalPageCount) {
			displayPageIndicator(0, PAGE_INDICATOR_COUNT_PER_PAGE, false, true);
		}
		// 5개보다 적으면
		else {
			displayPageIndicator(0, mTotalPageCount + 1, false, false);
		}
		// 맨 처음에는 1번에 셋팅
		setCurrentPageCount(0);
	}

	/**
	 * @param from
	 * @param count
	 * @param hasPrev
	 * @param hasNext
	 */
	private void displayPageIndicator(int from, int count, boolean hasPrev,
			boolean hasNext) {
		mFromCount = from;
		for (int i = 0; i < count; i++) {
			mCurrentCountTv = (TextView) mLayout.findViewById(mContext
					.getResources().getIdentifier(
							"page_indicator_view_go_" + i, "id",
							mContext.getPackageName()));
			mCurrentCountTv.setText(" " + Integer.toString(from + i + 1) + " ");
			mCurrentCountTv.setVisibility(View.VISIBLE);
			mCurrentCountTv.setOnClickListener(mClickListener);
		}
		for (int i = count; i < PAGE_INDICATOR_COUNT_PER_PAGE; i++) {
			mCurrentCountTv = (TextView) mLayout.findViewById(mContext
					.getResources().getIdentifier(
							"page_indicator_view_go_" + i, "id",
							mContext.getPackageName()));
			mCurrentCountTv.setVisibility(View.GONE);
		}
		if (hasPrev) {
			mGoFirst.setVisibility(View.VISIBLE);
			mGoPrev.setVisibility(View.VISIBLE);
			mGoFirst.setOnClickListener(mClickListener);
			mGoPrev.setOnClickListener(mClickListener);
		}
		if (hasNext) {
			mGoNext.setVisibility(View.VISIBLE);
			mGoLast.setVisibility(View.VISIBLE);
			mGoNext.setOnClickListener(mClickListener);
			mGoLast.setOnClickListener(mClickListener);
		}
	}

	/**
	 * 현재 위치를 선택
	 * 
	 * @param location
	 */
	public void setCurrentPageCount(int location) {
		// id는 선택된 위치
		int id = location % PAGE_INDICATOR_COUNT_PER_PAGE;
		for (int i = 0; i < PAGE_INDICATOR_COUNT_PER_PAGE; i++) {
			mCurrentCountTv = (TextView) mLayout.findViewById(mContext
					.getResources().getIdentifier(
							"page_indicator_view_go_" + i, "id",
							mContext.getPackageName()));
			if (id == i) {
				mCurrentCountTv.setTextColor(Color.BLUE);
			} else {
				mCurrentCountTv.setTextColor(Color.BLACK);
			}
		}
	}

	public void setPageIndicatorClickListener(PageIndicatorClickListener l) {
		mPageIndicatorClickListener = l;
	}

}
